/* 
 * Copyright (c) 2008 Allurent, Inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.allurent.coverage.model
{
    import mx.collections.ArrayCollection;
    
    /**
     * Coverage model for an entire application. 
     */
    [Bindable]
    public class CoverageModel extends SegmentModel
    {
        /**
         * Construct a new CoverageModel. 
         */
        public function CoverageModel()
        {
            name = "[Application]";
            
            // Set our children to an empty array collection so that this node will be expandable
            // from the get-go in a tree-style view.  The expectation is that if empty, it will be
            // filled later.
            //
            children = new ArrayCollection();
        }
        
        override public function createChild():SegmentModel
        {
            return new PackageModel();
        }
        
        /**
         * Record the reception of a CoverageElement with an associated element count
         * by bumping the execution count of a LineModel resolved from that element's path.
         * If the element couldn't be resolved, tough cookies.
         */
        public function recordCoverageElement(element:CoverageElement, count:uint, constrainToModel:Boolean):void
        {
            var model:LineModel = resolvePath(element.path, !constrainToModel) as LineModel;
            if (model != null)
            {
                if (model.executionCount == 0)
                {
                    model.element = element;
                }
                model.addExecutionCount(count);
            }
        }
        
        /**
         * Add a CoverageElement whose existence was recorded in metadata generated by the compiler.
         * This establishes some LineModel as present within the overall coverage model. 
         */
        public function addCoverageElement(element:CoverageElement):void
        {
            var model:SegmentModel = resolvePath(element.path, true);

            // LineModels that have an associated pathname need to communicate
            // that pathname to the associated ClassModel.  Kind of a hack here
            // but needed because we don't get a clean class-to-pathname association
            // in the metadata.
            //
            if (model is LineModel && element.pathname != null)
            {
                var classModel:ClassModel = ClassModel(model.parent.parent);
                classModel.pathname = element.pathname;
            }
        }
        
        override protected function createXmlElement():XML
        {
            return <applicationCoverage/>;
        }
    }
}
